package com.hisense.ovcloud.warehouse.service;

import com.hisense.ovcloud.commons.configs.base.BaseResponse;
import com.hisense.ovcloud.warehouse.Constants;
import com.hisense.ovcloud.warehouse.config.ReadShardingTablesConfig;
import com.hisense.ovcloud.warehouse.config.ShardingJDBCExecutor;
import com.hisense.ovcloud.warehouse.dto.req.GetInfoReq;
import com.hisense.ovcloud.warehouse.entity.julinkwarehouse.DeviceWarehouse;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class BasicDataService {

    private final ShardingJDBCExecutor shardingJDBCExecutor;

    public BasicDataService(ShardingJDBCExecutor shardingJDBCExecutor) {

        this.shardingJDBCExecutor = shardingJDBCExecutor;
    }

    public Mono<BaseResponse> getInfoTotal(Mono<GetInfoReq> req) {
        return req.flatMap(r -> Mono.just(ReadShardingTablesConfig.checkMajorDomain(r.getMajorDomainId()))
                        .zipWhen(eq -> {
                            if (!eq) {
                                return Mono.just(BaseResponse.builder().code(Constants.RESULT_FAILED).build());
                            } else {
                                StringBuilder sql = new StringBuilder(Constants.JulinkWarehouse.SELECT_COUNT_BY_SUB_DOMAIN_PREFIX);
                                if(r.getStatus() > 0){
                                    sql.append(String.format(" and status = %d ", r.getStatus()));
                                }
                                if(StringUtils.isNotEmpty(r.getWifiId())){
                                    sql.append(String.format(" and device_id = '%s' ", r.getWifiId()));
                                }
                                if(r.getCreateTimeStart() != null){
                                    sql.append(String.format(" and create_time >= '%s' ", r.getCreateTimeStart()));
                                }
                                if(r.getCreateTimeEnd() != null){
                                    sql.append(String.format(" and create_time <= '%s' ", r.getCreateTimeEnd()));
                                }
                                sql.append(Constants.JulinkWarehouse.SELECT_COUNT_BY_SUB_DOMAIN_SUFFIX);
                                String finalSql = sql.toString()
                                        .replaceAll(Constants.JulinkWarehouse.SUB_DOMAIN_TO_REPLACE, r.getSubDomainId())
                                        .replaceAll(Constants.JulinkWarehouse.LOGIC_TABLE_NAME, Constants.JulinkWarehouse.BASE_LOGIC_TABLE_NAME + r.getMajorDomainId());
                                log.debug("getDeviceBySubDomain sql: {}", finalSql);
                                return Mono.just(BaseResponse.builder()
                                        .data(shardingJDBCExecutor.directExecuteQueryCount(finalSql)).build());
                            }
                        }, (x, y) -> y)
        );
    }

    public Mono<BaseResponse> getDeviceBySubDomain(Mono<GetInfoReq> req) {
        return req.flatMap(r -> Mono.just(ReadShardingTablesConfig.checkMajorDomain(r.getMajorDomainId()))
                        .zipWhen(eq -> {
                            if (!eq) {
                                return Mono.just(BaseResponse.builder().code(Constants.RESULT_FAILED).build());
                            } else {
                                StringBuilder sql = new StringBuilder(Constants.JulinkWarehouse.SELECT_BY_SUB_DOMAIN_PREFIX);
                                if(r.getStatus() > 0){
                                    sql.append(String.format(" and status = %d ", r.getStatus()));
                                }
                                if(StringUtils.isNotEmpty(r.getWifiId())){
                                    sql.append(String.format(" and device_id = '%s' ", r.getWifiId()));
                                }
                                if(r.getCreateTimeStart() != null){
                                    sql.append(String.format(" and create_time >= '%s' ", r.getCreateTimeStart()));
                                }
                                if(r.getCreateTimeEnd() != null){
                                    sql.append(String.format(" and create_time <= '%s' ", r.getCreateTimeEnd()));
                                }
                                sql.append(Constants.JulinkWarehouse.SELECT_BY_SUB_DOMAIN_SUFFIX);
                                String finalSql = sql.toString()
                                        .replaceAll(Constants.JulinkWarehouse.SUB_DOMAIN_TO_REPLACE, r.getSubDomainId())
                                        .replaceAll(Constants.JulinkWarehouse.LIMIT_TO_REPLACE, String.valueOf(r.getPageSize()))
                                        .replaceAll(Constants.JulinkWarehouse.OFFSET_TO_REPLACE, String.valueOf((r.getPageNum() - 1) * r.getPageSize()))
                                        .replaceAll(Constants.JulinkWarehouse.LOGIC_TABLE_NAME, Constants.JulinkWarehouse.BASE_LOGIC_TABLE_NAME + r.getMajorDomainId());
                                log.debug("getDeviceBySubDomain sql: {}", finalSql);
                                return Mono.just(BaseResponse.builder()
                                        .data(shardingJDBCExecutor.directExecuteQueryAndMapToBeanV2(finalSql
                                                , DeviceWarehouse.class)).build());
                            }
                        }, (x, y) -> y)
        );
    }
}
